package com.yl.immodule.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.unionbroad.greendao.User;
import com.yl.immodule.R;
import com.yl.immodule.databinding.MaterialSelectDialogBinding;
import com.yl.immodule.databinding.ItemContactMaterialBinding;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MaterialSelectDialog extends Dialog {
    private MaterialSelectDialogBinding binding;
    private ContactAdapter adapter;
    private List<User> allUsers = new ArrayList<>();

    public MaterialSelectDialog(@NonNull Context context) {
        super(context, R.style.MaterialDialogStyle);
        initView();
    }

    private void initView() {
        binding = MaterialSelectDialogBinding.inflate(LayoutInflater.from(getContext()));
        setContentView(binding.getRoot());

        // 设置对话框宽度和高度
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = (int) (getContext().getResources().getDisplayMetrics().widthPixels * 0.9);  // 设置为屏幕宽度的90%
            params.height = (int) (getContext().getResources().getDisplayMetrics().heightPixels * 0.7);
            window.setAttributes(params);
        }

        // 设置RecyclerView
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new ContactAdapter();
        binding.recyclerView.setAdapter(adapter);

        // 设置按钮点击事件
        binding.btnCancel.setOnClickListener(v -> dismiss());
        binding.btnConfirm.setOnClickListener(v -> dismiss());
    }

    public void setUsers(List<User> users) {
        if (users != null) {
            Collections.sort(users, (o1, o2) -> {
                String name1 = o1.getName() != null ? o1.getName() : "";
                String name2 = o2.getName() != null ? o2.getName() : "";
                return name1.compareTo(name2);
            });
            this.allUsers = new ArrayList<>(users);
            adapter.setData(users);
        }
    }

    public List<User> getSelectedUsers() {
        return adapter.getSelectedUsers();
    }

    private static class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ViewHolder> {
        private List<User> users = new ArrayList<>();
        private final Map<Long, User> selectedUsers = new HashMap<>();

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            ItemContactMaterialBinding binding = ItemContactMaterialBinding.inflate(
                LayoutInflater.from(parent.getContext()), parent, false
            );
            return new ViewHolder(binding);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            User user = users.get(position);
            holder.binding.setUser(user);
            holder.binding.setChecked(selectedUsers.containsKey(user.getNumber()));
            
            holder.binding.getRoot().setOnClickListener(v -> {
                boolean isChecked = !selectedUsers.containsKey(user.getNumber());
                if (isChecked) {
                    selectedUsers.put(user.getNumber(), user);
                } else {
                    selectedUsers.remove(user.getNumber());
                }
                holder.binding.setChecked(isChecked);
                holder.binding.executePendingBindings();
            });
            
            holder.binding.executePendingBindings();
        }

        @Override
        public int getItemCount() {
            return users.size();
        }

        public void setData(List<User> users) {
            this.users = new ArrayList<>(users);
            notifyDataSetChanged();
        }

        public List<User> getSelectedUsers() {
            return new ArrayList<>(selectedUsers.values());
        }

        static class ViewHolder extends RecyclerView.ViewHolder {
            final ItemContactMaterialBinding binding;

            ViewHolder(ItemContactMaterialBinding binding) {
                super(binding.getRoot());
                this.binding = binding;
            }
        }
    }
} 